<template>
  <div class="app-container">
    <div class="card-box">
      <el-button type="primary" @click="handleToAdd">添加账单</el-button>
    </div>
    <div class="card-box">
      <el-table
        v-loading="supplierAccountStore.loading.list || supplierAccountStore.loading.update"
        :data="supplierAccountStore.list"
        row-id="id"
      >
        <el-table-column label="ID" prop="id" />
        <el-table-column label="供应商">
          <template #default="{ row }">
            {{ row.supplier ? row.supplier.name : "无" }}
          </template>
        </el-table-column>
        <el-table-column label="订单编号" prop="code" />
        <el-table-column label="状态" prop="status">
          <template #default="{ row }">
            <el-tag v-if="row.status == 'freeze'" type="danger">冻结</el-tag>
            <el-tag v-if="row.status == 'normal'" type="info">待结算</el-tag>
            <el-tag v-if="row.status == 'completed'" type="success">已结算</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="金额" prop="amount" />
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleToEdit(row)">编辑</el-button>
            <el-popconfirm title="是否继续删除？" @confirm="handleDelete(row)">
              <template #reference>
                <el-button type="danger" link>删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="params.page"
        v-model:page-size="params.size"
        style="margin-top: 10px"
        background
        :total="supplierAccountStore.totalCount"
        @change="supplierAccountStore.fetchList(params)"
      />
    </div>
    <Form
      v-model="visible"
      :type="formType"
      :form="currentForm"
      @confirm="supplierAccountStore.fetchList(params)"
    />
  </div>
</template>

<script setup lang="ts">
import {
  type SupplierAccountListQuery,
  type SupplierAccountForm,
  type SupplierAccountItemType,
} from "@/api/supplier/account";
import { useSupplierAccountStoreHook } from "@/store/modules/supplier/account";
import Form from "./components/form.vue";
import type { FormType } from "./components/form.vue";

const supplierAccountStore = useSupplierAccountStoreHook();

const visible = ref(false);

const formType = ref<FormType>("add");

const currentForm = ref<SupplierAccountForm>({
  is_active: true,
  code: "",
  amount: "",
  status: "freeze",
});

const params = reactive<SupplierAccountListQuery>({
  page: 1,
  size: 10,
});

function handleToAdd() {
  formType.value = "add";
  visible.value = true;
}

async function handleDelete(row: SupplierAccountItemType) {
  await supplierAccountStore.fetchDelete(row.id);
  await supplierAccountStore.fetchList(params);
}

function handleToEdit(row: SupplierAccountItemType) {
  console.log(row);
  formType.value = "edit";
  currentForm.value = {
    id: row.id,
    is_active: row.is_active,
    status: row.status,
    code: row.code,
    amount: row.amount,
    supplier: row.supplier ? row.supplier.id : undefined,
    merchant: row.merchant ? row.merchant.id : undefined,
  };
  visible.value = true;
}

onMounted(() => {
  supplierAccountStore.fetchList(params);
});
</script>

<style scoped lang="scss"></style>
